package com.cy.pj.sys.controller;

import com.cy.pj.common.pojo.JsonResult;
import com.cy.pj.sys.pojo.SysMenu;
import com.cy.pj.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 创建SysMenuController类型,通过此对象处理客户端请求,并将结果响应到客户端?
 * 1)处理什么请求?
 * 1.1)Get
 * 1.1.1)url:   /menu/
 * 1.1.2)url:   /menu/{id}
 * 1.2)Post  url:/menu/
 * 1.3)Put   url:/menu/
 * 1.4)Delete
 * 2)参数处理
 * 2.1)@PathVariable  (/meenu/{id})
 * 2.2)@RequestBody  ({id:10,name:""...})
 * 2.3)@ReuqestParam ("/?id=10&....")
 * 3)响应什么数据?
 * 3.1)jsonResult
 * 3.2)...
 * 4)异常数据如何处理?
 * 4.1)Controller方法内部(每个方法内部都定义异常处理,try{}catch(){})
 * 4.2)Controller类的内部(Controller类的内部定义专门的异常处理方法,@ExceptionHandle)
 * 4.3)Controller类的外部定义全局异常处理(@RestControllerAdvice,..推荐)
 * 5)控制Controller方法的调用?(HandlerInterceptor)
 *
 */
@CrossOrigin
@RestController
@RequestMapping("/menu/")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @PutMapping
    public JsonResult doUpdateMenu(@RequestBody SysMenu menu){
        sysMenuService.updateMenu(menu);
        return new JsonResult("Update Ok");
    }

    @GetMapping("{id}")
    public JsonResult doFindById(@PathVariable Integer id){
        return new JsonResult(sysMenuService.findById(id));
    }

    @PostMapping
    public JsonResult doSaveMenu(@RequestBody SysMenu menu){
        sysMenuService.saveMenu(menu);
        return new JsonResult("Save OK");
    }

    @GetMapping("treeNodes")
    public JsonResult doFindMenuTreeNodes(){
        return new JsonResult(sysMenuService.findMenuTreeNodes());
    }

    @GetMapping
    public JsonResult doFindMenus(){
        return new JsonResult(sysMenuService.findMenus());
    }
}
